package com.sihan.framework.etm.controller;


import com.framework.annotation.BusinessLog;
import com.framework.excel.ExcelHelper;
import com.framework.model.ApiResult;
import com.sihan.framework.etm.common.PageRequest;
import com.framework.model.PageResult;
import com.sihan.framework.etm.dto.LogLookDTO;
import com.sihan.framework.etm.entity.Log;
import com.sihan.framework.etm.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "日志管理")
@RestController
@RequestMapping("/system/v0/log")
public class AdminLogController {

  @Autowired
  private LogService logService;

  @GetMapping("/page")
  @ApiOperation("分页检索系统日志")
  @RequiresPermissions("sys:log:view")
  public ApiResult<PageResult<Log>> logPages(PageRequest pageRequest,Date startTime,Date endTime, String ip, String operation, Integer type) {
    PageResult<Log> result = logService.selectPage(pageRequest,startTime,endTime,ip,operation,type);
    return ApiResult.success(result);
  }

  @ApiOperation("系统日志备份")
  @BusinessLog("系统日志备份")
  @GetMapping("/sysBackup")
  @RequiresPermissions("sys:log:back")
  public void sysBackup(HttpServletResponse response) {
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/vnd.ms-excel");
    try {
      response.setHeader("Content-Disposition", "attachment;filename="
              .concat(String.valueOf(URLEncoder.encode("系统日志.xls", "UTF-8"))));
      ExcelHelper.write(logService.findLogAllByType(0), response.getOutputStream(), "sihankeji");
    } catch (UnsupportedEncodingException e) {
    	log.error("异常",e);
    } catch (IOException e) {
    	log.error("异常",e);
    }
  }

  @ApiOperation("业务日志备份")
  @BusinessLog("业务日志备份")
  @GetMapping("/busBackup")
  @RequiresPermissions("sys:log:back")
  public void busBackup(HttpServletResponse response) {
    response.setCharacterEncoding("utf-8");
    response.setContentType("application/vnd.ms-excel");
    try {
      response.setHeader("Content-Disposition", "attachment;filename="
              .concat(String.valueOf(URLEncoder.encode("业务日志.xls", "UTF-8"))));
      ExcelHelper.write(logService.findLogAllByType(1), response.getOutputStream(), "sihankeji");
    } catch (UnsupportedEncodingException e) {
    	log.error("异常",e);
    } catch (IOException e) {
    	log.error("异常",e);
    }
  }


  @ApiOperation("获取所有操作类型")
  @GetMapping("/find_op")
  @RequiresPermissions("sys:log:look")
  public ApiResult<?> findOperas(Integer logType) {
    if(logType == null || (logType.intValue() != 0 && logType.intValue() != 1)) {
      return ApiResult.fail("日志类型错误");
    }
    return ApiResult.success(logService.findOperas(logType));
  }

  @ApiOperation("获取所有的行为主体")
  @GetMapping("/find_opusers")
  @RequiresPermissions("sys:log:look")
  public ApiResult<?> findOperaUsers(Integer logType) {
    if(logType == null || (logType.intValue() != 0 && logType.intValue() != 1)) {
      return ApiResult.fail("日志类型错误");
    }
    return ApiResult.success(logService.findOperaUsers(logType));
  }

  @ApiOperation("通过行为主体统计日志")
  @GetMapping("/look_result")
  @RequiresPermissions("sys:log:look")
  public ApiResult<?> lookResult(LogLookDTO dto) {
    if(dto == null || StringUtils.isBlank(dto.getCreateUser())) {
      return ApiResult.fail("行为主体不能为空");
    }

    if(dto.getLogType() == null || (dto.getLogType().intValue() != 0 && dto.getLogType().intValue() != 1)) {
      return ApiResult.fail("日志类型错误");
    }

    return ApiResult.success(logService.look(dto));
  }

  @ApiOperation("通过事件类型统计日志")
  @GetMapping("/look_result1")
  @RequiresPermissions("sys:log:look")
  public ApiResult<?> lookResult1(LogLookDTO dto) {
    if(dto == null || StringUtils.isBlank(dto.getOperation())) {
      return ApiResult.fail("事件类型不能为空");
    }

    if(dto.getLogType() == null || (dto.getLogType().intValue() != 0 && dto.getLogType().intValue() != 1)) {
      return ApiResult.fail("日志类型错误");
    }

    return ApiResult.success(logService.look1(dto));
  }

  /*@PostMapping("/log/delete")
  @ApiOperation("删除系统日志")
  @BusinessLog("删除系统日志")
  @RequiresPermissions("sys:log:delete")
  public ApiResult deleteLog(@Valid @RequestBody List<Long> logIds) {
    logService.deleteLogByIds(logIds);
    return ApiResult.ok();
  }*/

}
